﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Web
{
    public partial class Charge : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["username"] = "fg";

                if (null != Session["username"])
                {
                    Label_State.Text = "已登录";
                    Label_User.Text = Session["username"].ToString();

                    DAL.UserInfoDAL userInfoDAL = (DAL.UserInfoDAL)DALFAC.DataAccess.CreateUserInfo();
                    DAL.AccountsDAL accountsDal = (DAL.AccountsDAL)DALFAC.DataAccess.CreateAccounts();

                    if (null != userInfoDAL && null != accountsDal)
                    {
                        Model.UserInfo userInfoModel = userInfoDAL.userNameUserInfo(Session["username"].ToString());
                        int userId = userInfoModel.getUI_userId();
                        Session["userId"] = userId;

                        Label_UserName.Text = Session["username"].ToString();
                        Model.Accounts accountModel = accountsDal.GetModelByUserId(userId);
                        Session["accountId"] = accountModel.getAC_accountId();
                        Label_Account.Text = accountModel.getAC_balance().ToString();

                    }
                }
                else
                {
                    Response.Redirect("Login.aspx");
                }
            }
        }
     

        protected void Search_Click(object sender, EventArgs e)
        {
            string userKey = TextBox_Search.Text;
            string url = "SearchResult.aspx?key=" + TextBox_Search.Text;
            Response.Redirect(url);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Session.Clear();
            Response.Redirect("Index.aspx");
        }

        protected void Charge_Click(object sender, EventArgs e)
        {
            bool validator = false;
            if(null != Session["accountId"])
            {              
                int accountId = Convert.ToInt32(Session["accountId"].ToString());
                BLL.ChargeBLL chargeBLL = (BLL.ChargeBLL)BLLFAC.DataAccess.CreateCharge();
                string cardnum = TextBox3.Text;
                if (0 != cardnum.Length) 
                {
                    validator = chargeBLL.cardCharge(accountId, cardnum);
                }
                if (validator)
                {
                    Label4.Text = "充值成功";
                    DAL.AccountsDAL accountsDal = (DAL.AccountsDAL)DALFAC.DataAccess.CreateAccounts();
                    Model.Accounts accountModel = accountsDal.GetModel(accountId);
                    Label_Account.Text = accountModel.getAC_balance().ToString();
                    TextBox3.Text = "";
                }
                else
                {
                    Label4.Text = "充值失败";
                    TextBox3.Text = "";
                }
            }

            
             
        }
    }
}